package com.spotimage.eosps.test;


import java.io.InputStream;
import org.vast.cdm.common.DataComponent;
import org.vast.ows.sps.DescribeTaskingResponse;
import org.vast.ows.sps.GetFeasibilityRequest;
import org.vast.ows.sps.GetFeasibilityResponse;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport.RequestStatus;
import org.vast.util.DateTime;
import org.vast.xml.DOMHelper;
import com.spotimage.eosps.EOReportHelper;
import com.spotimage.eosps.EOConstants.AcqStatus;
import com.spotimage.eosps.EOConstants.ReportType;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import junit.framework.TestCase;


public class TutorialExamples extends TestCase
{
	protected SPSUtils utils = new SPSUtils();
	
	
	public void testReceivingGFRequest() throws Exception
	{
		InputStream inputStream = getClass().getResourceAsStream("GetFeasibility_OPT1_XML.xml");
		
		SPSUtils utils = new SPSUtils();
		
		// read describe tasking response for tasking parameters
		String respUrl = getClass().getResource("DescribeTaskingResponse_OPT1.xml").toString();
		DOMHelper dom = new DOMHelper(respUrl, false);
		DescribeTaskingResponse resp = utils.readDescribeTaskingResponse(dom.getRootElement());
		
		// get Tasking parameters description
		DataComponent taskingParams = resp.getTaskingParameters();
		
		// read get feasibility request using these parameters
		dom = new DOMHelper(inputStream, false);
		GetFeasibilityRequest request = (GetFeasibilityRequest)utils.readParameterizedRequest(dom, dom.getRootElement(), taskingParams);
		
		// validate request
		// this will send exceptions if a parameter value is invalid
		request.validate();
		
		// ... process request ...
	}
	
	
	public void testSendingGFResponse() throws Exception
	{
		GetFeasibilityResponse gfResponse = new GetFeasibilityResponse();
		gfResponse.setVersion("2.0.0");
		gfResponse.setMaxResponseDelay(3600);
		gfResponse.getReport().setTitle("Automatic Feasibility");
		gfResponse.getReport().setTaskID("F001");
		gfResponse.getReport().setSensorID("urn:spot:sensors:SPOT5:HRG");
		gfResponse.getReport().setRequestStatus(RequestStatus.Accepted);
		gfResponse.getReport().touch();
		
		// instantiate helper
		EOReportHelper helper = new EOReportHelper(gfResponse, ReportType.FEASIBILITY);
		helper.setExpirationDelay(3600*24*2); // valid 48h
		helper.setEstimatedCost(20000);
		
		// segments
		int numSegments = 5;		
		helper.setNumSegments(numSegments);		
		for (int s=0; s<numSegments; s++)
		{
			helper.loadSegment(s);
			
			helper.setID("SEG_00" + (s+1));
			helper.setObjectStatus(AcqStatus.ACQUIRED);
			helper.setPlatformID("urn:ogc:id:CEOS:platform:SPOT5");
			helper.setPlatformName("SPOT-5");
			helper.setInstrumentID("urn:ogc:id:CEOS:instrument:SPOT5:HRG1");
			helper.setInstrumentName("HRG1");
			helper.setInstrumentMode("PAN");
			helper.setAcquisitionStart(new DateTime((2000+s) + "-05-22T10:45:12Z"));
			helper.setAcquisitionStop(new DateTime((2000+s) + "-06-22T10:45:20Z"));
			helper.setElevationIncidence(13.5);
			
			// ROI
			GeometryFactory fac = new GeometryFactory();
			LinearRing outer = fac.createLinearRing(new Coordinate[]{
					new Coordinate(0+s, 0), new Coordinate(10+s, 0),
					new Coordinate(10+s, 10), new Coordinate(0+s, 10),
					new Coordinate(0+s, 0)});
			helper.setFootprint(fac.createPolygon(outer, null));
		}
		
		// cells
		int numCells = 3;
		helper.setNumGridCells(numCells);
		for (int s=0; s<numCells; s++)
		{
			helper.loadGridCell(s);
			
			helper.setID("CELL_00" + (s+1));
			helper.setObjectStatus(AcqStatus.ACQUIRED);
			helper.setSuccessRate(80+s);
			helper.setEstimatedSuccessDate(new DateTime((2000+s) + "-02-22T10:45:12Z"));
			helper.setNextAttemptDate(new DateTime((2000+s) + "-01-22T10:45:12Z"));
			
			// ROI
			GeometryFactory fac = new GeometryFactory();
			LinearRing outer = fac.createLinearRing(new Coordinate[]{
					new Coordinate(0+s, 0), new Coordinate(10+s, 0),
					new Coordinate(10+s, 10), new Coordinate(0+s, 10),
					new Coordinate(0+s, 0)});
			helper.setFootprint(fac.createPolygon(outer, null));
		}	
		
		helper.writeReportInfo();
		SPSUtils utils = new SPSUtils();
		utils.writeXMLResponse(System.out, gfResponse);
	}
}
